package ge.modules.drawables;


import ge.engine.Engine;
import ge.engine.Log;
import ge.engine.PositionableArea;
import ge.exceptions.DrawEngineException;
import ge.exceptions.FunctionNonSuported;
import ge.lowlevel.DrawEngine;

 
 

 

public class SpriteModule extends DrawableObject implements PositionableArea
{
	
	 
	private double imgWidth = 0, imgHeight = 0;
	private String imgUrl = "";
	private Animation currentAnimation = null;
	private Animation nextAnimation;
	
	/**
	 * 	Constructor of Sprites
	 * @param width	Width of one frame
	 * @param height Height of one frame
	 */
	public SpriteModule(double width, double height)
	{
		super(width,height);
		this.nextAnimation = null;
	}

	/**
	 * 	Sets the image whit all the frames of the sprite
	 * 	Constructs a default animation with the whole sprite
	 * @param imgUrl	Position where you can find the image
	 * @param imgWidth	Width of the sprite
	 * @param imgHeight Height of the sprite
	 */
	public void setImage(String imgUrl, int imgWidth, int imgHeight)
	{
		this.imgHeight = imgHeight;
		this.imgWidth = imgWidth;
		this.imgUrl = imgUrl;
		try {
			currentAnimation = defaultAnimation();
		} catch (DrawEngineException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	/**
	 * 	Calculates the number of frames depending on the size of the sprite
	 * and the size of one frames
	 * @return Number of frames in this sprite
	 */
	public int getNumFrames()
	{
		int numFrames = (int)(imgWidth/imageSquare.getWidth()); //Division entera
		numFrames *= (imgHeight/imageSquare.getHeight());
		return numFrames;
	}
	/**
	 * 	Get the default animation, one animation with all the frames of the sprite and loop set on
	 * @return	Animation
	 * @throws DrawEngineException If there is not a sprite associate I'll be raised an exception
	 */
	public Animation defaultAnimation() throws DrawEngineException
	{
		
		Animation def = null;
		int numFrames = getNumFrames();
		int[] listaframes = new int[numFrames];
		
		for(int i = 0; i < numFrames; i++)
			listaframes[i] = i;
		
		def  = new Animation(listaframes,Engine.getFps());
		if(listaframes.length > 1) //Si no hay mas de un frame no hace falta el loop
			def.setLoop(true);
		return def;
	}
	
	/**
	 * @return Current animation
	 */
	public Animation getAnimation()
	{
		return currentAnimation;
	}
	 
	/**
	 * 	Changes the animation of the sprite
	 * @param a Animation
	 */
	public void setAnimation(Animation a)
	{
		currentAnimation = a;
	}
	/**
	 * 	Changes the animation of the sprite when the current finishes
	 *  It won't have effect if a is the current animation.
	 * @param a Animation
	 */
	public void setAnimationAfterFinish(Animation a)
	{
		if(currentAnimation != a)
			nextAnimation = a;
	}
	
	@Override
	public void update() 
	{
		super.update();
		
		if(nextAnimation != null && currentAnimation.isFinish())
		{
			currentAnimation = nextAnimation;
			nextAnimation =  null;
		}
		
	}

	@Override
	public void draw()
	{
		int frame;
		double sy, sx, desp;
		if(imgUrl != "")
		{
			
			frame = currentAnimation.nextFrame();
		 
			if(!isVisible())
				return;
			
			if(!isInsideScreen())
				return;
			try {
				DrawEngine.get().setLayer(layer);
				//if(this.getNumFrames() == 1)
				//{
				//	DrawEngine.get().drawImage(imgUrl, getRelativeX(), getRelativeY());
				//}
				//else
				{
					desp =  frame * imageSquare.getWidth();	
					sy = (int) Math.floor(desp/imgWidth);
					sx = desp%imgWidth;
					sy *= imageSquare.getHeight();
					//Log.write("("+getRelativeX()+","+getRelativeY()+") ("+relativeWidth()+","+relativeHeigth()+") ("+startDrawX()+","+startDrawY()+")");
					
						
						DrawEngine.get().drawFrame(imgUrl, getRelativeX(), getRelativeY(), relativeWidth(), relativeHeigth(), sx+startDrawX(), sy+startDrawY(), relativeWidth(), relativeHeigth());
					
				}
			} catch (FunctionNonSuported e) {
				e.printStackTrace();
			 
			}// catch (Exception e) {
				//this.draw();
			//}
		 
		}
	}
	/**
	 * 
	 * @return The width of all the image, not the frame
	 */
	public double getImgWidth() {
		return imgWidth;
	}
	/**
	 * 
	 * @return The Height of all the image, not the frame
	 */
	public double getImgHeight() {
		return imgHeight;
	}
	/**
	 * 	You can't resize the frame area indicate in the constructor
	 * 	function
	 */
	@Override
	public void setArea(double width, double height) 
	{
	}
	
	
}
